package test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import rmiClient.ManagementClient;
import server.BillingServerSecure;


import domain.Auction;

public class LoadTestComponent {

//	private static final int maxClients = 100;
	
	private static ExecutorService threadPool;
	private ArrayList<TestClientThread> threads = new ArrayList<TestClientThread>();
	private Timer timer;
	
	static int clients;
	static int auctionsPerMin;
	static int auctionDuration;
	static int updateIntervalSec;
	static int bidsPerMin;
	static int port;
	static String host;
	
	
	private LoadTestComponent(){
		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		String[] String;
		
//		String[] server = new String[2];
//		String analyticsName = "analyticsServer";
		String billingName = "billingServer";
//		ManagementClient mg = new ManagementClient(server);
		if(args.length != 3){
			return;
		}
		host = args[0];
		port = Integer.parseInt(args[1]);
		String analyticsName = args[2];

		
		try {
			Properties props = new Properties();
			java.io.InputStream is = ClassLoader.getSystemResourceAsStream("./loadtest.properties");
			props.load(is);

			if (is != null) {

				try {										
					clients = Integer.parseInt(props.getProperty("clients"));
					auctionsPerMin = Integer.parseInt(props.getProperty("auctionsPerMin"));
					auctionDuration = Integer.parseInt(props.getProperty("auctionDuration"))*1000;
					updateIntervalSec = Integer.parseInt(props.getProperty("updateIntervalSec"));
					bidsPerMin = Integer.parseInt(props.getProperty("bidsPerMin"));	
					
					threadPool = Executors.newCachedThreadPool();
					
					
					LoadTestComponent loadtestcomponent = new LoadTestComponent();
					loadtestcomponent.run();
					
					
				} finally {
					try {
						is.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} else {
				System.err.println("Properties file not found!");
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		

	}

	private void run() {
		//clientthreads bekommen paramter von propertyfile ..
		
		try {
			Properties props = new Properties();
			java.io.InputStream is = ClassLoader.getSystemResourceAsStream("./registry.properties");
			props.load(is);

			if (is != null) {

				try {										
					String host = props.getProperty("registry.host");
//					port = Integer.parseInt(props.getProperty("registry.port"));
					System.out.println("clients: "+clients);

//					System.out.println("port: "+port+", host: "+host);
					
					for(int i=0;i<clients;i++) {
						TestClientThread newThread = new TestClientThread(this, host, port, auctionsPerMin, auctionDuration, updateIntervalSec, bidsPerMin, "client"+i);
						threads.add(newThread);
						threadPool.execute(newThread);
											
					}						
					
					threadPool.shutdown();
					
				} finally {
					try {
						is.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			} else {
				System.err.println("Properties file not found!");
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		
	}
	
	public void closeThread(TestClientThread testClientThread){
		threads.remove(testClientThread);
	}
	


}
